<!DOCTYPE html>
<html lang="zh-Hans">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 4.2.1">
  <link rel="apple-touch-icon" sizes="180x180" href="/blog/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/blog/images/favicon-32x32-next.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/blog/images/favicon-16x16-next.png">
  <link rel="mask-icon" href="/blog/images/logo.svg" color="#222">

<link rel="stylesheet" href="/blog/css/main.css">


<link rel="stylesheet" href="/blog/lib/font-awesome/css/all.min.css">

<script id="hexo-configurations">
    var NexT = window.NexT || {};
    var CONFIG = {"hostname":"gujinyu102.gitee.io","root":"/blog/","scheme":"Muse","version":"7.8.0","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12,"onmobile":false},"copycode":{"enable":false,"show_result":false,"style":null},"back2top":{"enable":true,"sidebar":false,"scrollpercent":false},"bookmark":{"enable":false,"color":"#222","save":"auto"},"fancybox":false,"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"algolia":{"hits":{"per_page":10},"labels":{"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}},"localsearch":{"enable":false,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false},"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}}};
  </script>

  <meta name="description" content="#####超级偶数（SuperEven）是指每一位都是偶数的正整数，例如:0, 2, 4, 6, 8, 20, 22, 24, 26, 28, 40, …, 88, 200, 202, … 要求写一个函数，输入项数n，返回数列第n项的值。 说实话，这个题目整整花了我三天时间去思考（数学比较弱，大神见笑）#手动捂脸#。   其实到最后我还是没有完成这个Kata，因为作者要求用少于30个字符的代码解决">
<meta property="og:type" content="article">
<meta property="og:title" content="Code Kata: 超级偶数数列 javascript实现">
<meta property="og:url" content="https://gujinyu102.gitee.io/blog/2017/12/07/Code-Kata-%E8%B6%85%E7%BA%A7%E5%81%B6%E6%95%B0%E6%95%B0%E5%88%97-javascript%E5%AE%9E%E7%8E%B0/index.html">
<meta property="og:site_name" content="辜锦宇的博客">
<meta property="og:description" content="#####超级偶数（SuperEven）是指每一位都是偶数的正整数，例如:0, 2, 4, 6, 8, 20, 22, 24, 26, 28, 40, …, 88, 200, 202, … 要求写一个函数，输入项数n，返回数列第n项的值。 说实话，这个题目整整花了我三天时间去思考（数学比较弱，大神见笑）#手动捂脸#。   其实到最后我还是没有完成这个Kata，因为作者要求用少于30个字符的代码解决">
<meta property="og:image" content="https://gujinyu102.gitee.io/blog/.io//0.jpeg">
<meta property="og:image" content="https://gujinyu102.gitee.io/blog/.io//1.jpeg">
<meta property="og:image" content="https://gujinyu102.gitee.io/blog/.io//2.jpeg">
<meta property="og:image" content="https://gujinyu102.gitee.io/blog/.io//3.jpeg">
<meta property="og:image" content="https://gujinyu102.gitee.io/blog/.io//code.png">
<meta property="og:image" content="https://gujinyu102.gitee.io/blog/.io//4.jpeg">
<meta property="og:image" content="https://gujinyu102.gitee.io/blog/.io//5.jpeg">
<meta property="article:published_time" content="2017-12-07T09:13:52.000Z">
<meta property="article:modified_time" content="2020-06-09T09:30:08.339Z">
<meta property="article:author" content="辜锦宇">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://gujinyu102.gitee.io/blog/.io//0.jpeg">

<link rel="canonical" href="https://gujinyu102.gitee.io/blog/2017/12/07/Code-Kata-%E8%B6%85%E7%BA%A7%E5%81%B6%E6%95%B0%E6%95%B0%E5%88%97-javascript%E5%AE%9E%E7%8E%B0/">


<script id="page-configurations">
  // https://hexo.io/docs/variables.html
  CONFIG.page = {
    sidebar: "",
    isHome : false,
    isPost : true,
    lang   : 'zh-Hans'
  };
</script>

  <title>Code Kata: 超级偶数数列 javascript实现 | 辜锦宇的博客</title>
  






  <noscript>
  <style>
  .use-motion .brand,
  .use-motion .menu-item,
  .sidebar-inner,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-header { opacity: initial; }

  .use-motion .site-title,
  .use-motion .site-subtitle {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line-before i { left: initial; }
  .use-motion .logo-line-after i { right: initial; }
  </style>
</noscript>

</head>

<body itemscope itemtype="http://schema.org/WebPage">
  <div class="container use-motion">
    <div class="headband"></div>

    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="Toggle navigation bar">
      <span class="toggle-line toggle-line-first"></span>
      <span class="toggle-line toggle-line-middle"></span>
      <span class="toggle-line toggle-line-last"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/blog/" class="brand" rel="start">
      <span class="logo-line-before"><i></i></span>
      <h1 class="site-title">辜锦宇的博客</h1>
      <span class="logo-line-after"><i></i></span>
    </a>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
    </div>
  </div>
</div>




<nav class="site-nav">
  <ul id="menu" class="main-menu menu">
        <li class="menu-item menu-item-home">

    <a href="/blog/" rel="section"><i class="fa fa-home fa-fw"></i>Home</a>

  </li>
        <li class="menu-item menu-item-archives">

    <a href="/blog/archives/" rel="section"><i class="fa fa-archive fa-fw"></i>Archives</a>

  </li>
  </ul>
</nav>




</div>
    </header>

    
  <div class="back-to-top">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>


    <main class="main">
      <div class="main-inner">
        <div class="content-wrap">
          

          <div class="content post posts-expand">
            

    
  
  
  <article itemscope itemtype="http://schema.org/Article" class="post-block" lang="zh-Hans">
    <link itemprop="mainEntityOfPage" href="https://gujinyu102.gitee.io/blog/2017/12/07/Code-Kata-%E8%B6%85%E7%BA%A7%E5%81%B6%E6%95%B0%E6%95%B0%E5%88%97-javascript%E5%AE%9E%E7%8E%B0/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/blog/images/avatar.gif">
      <meta itemprop="name" content="辜锦宇">
      <meta itemprop="description" content="">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="辜锦宇的博客">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          Code Kata: 超级偶数数列 javascript实现
        </h1>

        <div class="post-meta">
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-calendar"></i>
              </span>
              <span class="post-meta-item-text">Posted on</span>

              <time title="Created: 2017-12-07 17:13:52" itemprop="dateCreated datePublished" datetime="2017-12-07T17:13:52+08:00">2017-12-07</time>
            </span>
              <span class="post-meta-item">
                <span class="post-meta-item-icon">
                  <i class="far fa-calendar-check"></i>
                </span>
                <span class="post-meta-item-text">Edited on</span>
                <time title="Modified: 2020-06-09 17:30:08" itemprop="dateModified" datetime="2020-06-09T17:30:08+08:00">2020-06-09</time>
              </span>

          

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">

      
        <p>#####超级偶数（SuperEven）是指每一位都是偶数的正整数，例如:<br>0, 2, 4, 6, 8, 20, 22, 24, 26, 28, 40, …, 88, 200, 202, …</p>
<p>要求写一个函数，输入项数n，返回数列第n项的值。</p>
<p>说实话，这个题目整整花了我三天时间去思考（数学比较弱，大神见笑）#手动捂脸#。  </p>
<p>其实到最后我还是没有完成这个Kata，因为作者要求用少于30个字符的代码解决，我的解决方案再怎么压缩也100个字符左右了。万念俱灰的我还是决定看别人的答案，发现一共有4个人解出来了，答案都一致，非常精妙。但是吹毛求疵的讲，标答也是有瑕疵的。卖个关子，先看看我的解答：</p>
<p>简单介绍一下思路:</p>
<p><strong>首先是找规律，既然每一位都是偶数，那么超级偶数的每一位都可以表示为￼ m=2x*10^N 的形式其中 0&lt;=x&lt;=4，N为位数，个位N=0，十位N=1以此类推。那么每个一个超级偶数M都可以表示为</strong></p>
<p>￼￼<img src="/blog/.io//0.jpeg" alt>    </p>
<p>通过观察发现，位数的变化规律为第 5^N-1 项到 5^N - 1项的位数位N。可以得到第a项的位数为</p>
<p>￼￼<img src="/blog/.io//1.jpeg" alt> *[]代表取整数位</p>
<p>同时发现每一位的变化也是有规律的，比如个位0、2、4、6、8为每5次一循环，而十位则为25次一循环，根据规律可以得到</p>
<p>￼￼<img src="/blog/.io//2.jpeg" alt> *[]代表取整数位</p>
<p>整理一下思路，把公式整合起来，我们设项数为a，第a项的值为M，第a项的位数为N</p>
<p>可以得到</p>
<p>￼￼￼<img src="/blog/.io//3.jpeg" alt> </p>
<p>接下来将公式转为js代码</p>
<p>￼￼￼<img src="/blog/.io//code.png" alt> </p>
<p>这里用到了字符串拼接而没有用数字加减，是考虑到了大数字情况下丢失精度的问题。</p>
<p>用’|0’进行位运算取整是为了缩短代码长度，但是后面发现在大数字情况下’|0’出现溢出问题，因此后面修改为了字符串截取小数点前面部分。</p>
<p>经过复杂的计算，我总算把答案拿出来了，然而30个字符解决问题是什么鬼。我想我肯定是从根本上就偏离的题主的想法，于是我怀着惴惴不安的心情浏览了提交上去的答案，结果简直让人震惊！不多说贴代码：</p>
<p><code>superEven=n=&gt;n.toString(5)*2</code></p>
<p>不得不说，我确实搞的过于复杂了，万万没想到这竟然是一个5进制的数组。我相信对数字敏感的大神肯定一眼就能看出来了吧。<br>但是结束了吗，并没有，当我测试两段代码时发现，在超大数据的情况下，纯数学运算是会丢失精度的，但是字符串拼接则不会</p>
<p>￼￼￼￼<img src="/blog/.io//4.jpeg" alt><br>￼<br>￼￼￼<img src="/blog/.io//5.jpeg" alt> </p>
<p>上面的结果为我的代码，下面为转5进制方法，node环境下运行速度上几乎可以忽略不计，两者都是小于1ms。<br>但是下面方法在结果超过17位时，开始丢失精度，并且在超过20位时，强制使用科学计数法</p>
<p>综上所述，标准答案确实是精妙绝伦，令人拍案叫绝，但是我自身的答案也是有可取之处的。    </p>
<p>说到底，做练习还是为了提升自我，虽然没能给出最优解，但是过程已经让我受益匪浅了。</p>

    </div>

    
    
    

      <footer class="post-footer">

        


        
    <div class="post-nav">
      <div class="post-nav-item">
    <a href="/blog/2016/12/08/e5-89-8d-e7-ab-af-e5-b7-a5-e7-a8-8b-e5-8c-96-e7-a0-94-e7-a9-b6-ef-bc-88-e4-b8-80-ef-bc-89/" rel="prev" title="前端工程化研究（一）">
      <i class="fa fa-chevron-left"></i> 前端工程化研究（一）
    </a></div>
      <div class="post-nav-item">
    <a href="/blog/2020/07/27/JavaScript/" rel="next" title="JavaScript">
      JavaScript <i class="fa fa-chevron-right"></i>
    </a></div>
    </div>
      </footer>
    
  </article>
  
  
  



          </div>
          

<script>
  window.addEventListener('tabs:register', () => {
    let { activeClass } = CONFIG.comments;
    if (CONFIG.comments.storage) {
      activeClass = localStorage.getItem('comments_active') || activeClass;
    }
    if (activeClass) {
      let activeTab = document.querySelector(`a[href="#comment-${activeClass}"]`);
      if (activeTab) {
        activeTab.click();
      }
    }
  });
  if (CONFIG.comments.storage) {
    window.addEventListener('tabs:click', event => {
      if (!event.target.matches('.tabs-comment .tab-content .tab-pane')) return;
      let commentClass = event.target.classList[1];
      localStorage.setItem('comments_active', commentClass);
    });
  }
</script>

        </div>
          
  
  <div class="toggle sidebar-toggle">
    <span class="toggle-line toggle-line-first"></span>
    <span class="toggle-line toggle-line-middle"></span>
    <span class="toggle-line toggle-line-last"></span>
  </div>

  <aside class="sidebar">
    <div class="sidebar-inner">

      <ul class="sidebar-nav motion-element">
        <li class="sidebar-nav-toc">
          Table of Contents
        </li>
        <li class="sidebar-nav-overview">
          Overview
        </li>
      </ul>

      <!--noindex-->
      <div class="post-toc-wrap sidebar-panel">
      </div>
      <!--/noindex-->

      <div class="site-overview-wrap sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
  <p class="site-author-name" itemprop="name">辜锦宇</p>
  <div class="site-description" itemprop="description"></div>
</div>
<div class="site-state-wrap motion-element">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
          <a href="/blog/archives/">
        
          <span class="site-state-item-count">5</span>
          <span class="site-state-item-name">posts</span>
        </a>
      </div>
  </nav>
</div>



      </div>

    </div>
  </aside>
  <div id="sidebar-dimmer"></div>


      </div>
    </main>

    <footer class="footer">
      <div class="footer-inner">
        

        

<div class="copyright">
  
  &copy; 
  <span itemprop="copyrightYear">2020</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">辜锦宇</span>
</div>
  <div class="powered-by">Powered by <a href="https://hexo.io/" class="theme-link" rel="noopener" target="_blank">Hexo</a> & <a href="https://muse.theme-next.org/" class="theme-link" rel="noopener" target="_blank">NexT.Muse</a>
  </div>

        








      </div>
    </footer>
  </div>

  
  <script src="/blog/lib/anime.min.js"></script>
  <script src="/blog/lib/velocity/velocity.min.js"></script>
  <script src="/blog/lib/velocity/velocity.ui.min.js"></script>

<script src="/blog/js/utils.js"></script>

<script src="/blog/js/motion.js"></script>


<script src="/blog/js/schemes/muse.js"></script>


<script src="/blog/js/next-boot.js"></script>




  















  

  

</body>
</html>
